//
//  UserManager.m
//  BCManagerVersion
//
//  Created by 吴 崧 on 14-2-23.
//  Copyright (c) 2014年 Jungle. All rights reserved.
//

#import "UserManager.h"
#import <objc/runtime.h>
#import "UserModel.h"
#define OBJC_STRINGIFY(x) @#x

//decode macro
#define decodeObject(x) x = [aDecoder decodeObjectForKey:OBJC_STRINGIFY(x)]
#define decodeBool(x) x = [aDecoder decodeBoolForKey:OBJC_STRINGIFY(x)]
#define decodeInteger(x) x = [aDecoder decodeIntegerForKey:OBJC_STRINGIFY(x)]
#define decodeFloat(x) x = [aDecoder decodeFloatForKey:OBJC_STRINGIFY(x)]
//encode macro
#define encodeObject(x) [aCoder encodeObject:x forKey:OBJC_STRINGIFY(x)]
#define encodeFloat(x) [aCoder encodeFloat:x forKey:OBJC_STRINGIFY(x)]
#define encodeInteger(x) [aCoder encodeInteger:x forKey:OBJC_STRINGIFY(x)]
#define encodeBool(x) [aCoder encodeBool:x forKey:OBJC_STRINGIFY(x)]

//Set Value For Key


#define EMPTY_NUMBER @(0)

#define USERDEFAULT [NSUserDefaults standardUserDefaults]





@implementation UserManager
@synthesize didLogin = _didLogin;

+(instancetype)sharedManager
{
    static UserManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSData *managerData = [USERDEFAULT objectForKey:USER_INFO_FLAG];
        if (managerData != nil) {
            sharedInstance = (UserManager *)[NSKeyedUnarchiver unarchiveObjectWithData:managerData];
        }else
        {
        sharedInstance = [[UserManager alloc] init];
        }
      
       
        [sharedInstance save];
    });
    return sharedInstance;
}


-(instancetype)init
{

    self = [super init];
    if (self) {
       [self setDefaultData];
    
        
           }
        return self;
}


-(void)configWithModel:(UserModel *)model
{
    _uid = model.uid;
    _passport = model.passport;
    _name = model.name;
    _avatar = model.avatar;
    _tAvatar = model.tAvatar;
    _iphoneId = model.iphoneId;
    _register_time = model.register_time;
    _city = model.city;
    _status = model.status;
    [self save];
}



-(void)setDefaultData
{
 
    _auth = EMPTY_STRING;
    _uid = EMPTY_NUMBER;
    _passport = EMPTY_STRING;
    _name = EMPTY_STRING;
    _avatar = @"userAvatar_placeholder";
    _tAvatar = EMPTY_STRING;
    _iphoneId = EMPTY_STRING;
    _register_time = EMPTY_STRING;
    _city = EMPTY_STRING;
    _status = EMPTY_NUMBER;
    
    _didLogin = NO;
    _canPush = YES;
    _canAlert = YES;
    _canSound = YES;
    _canVibrator = YES;
    _timeStamp = 0;
    [self save];
}



-(void)setAuth:(NSString *)auth
{
    if (auth == nil) {
        [self setDefaultData];

    }else if(![_auth isEqualToString:auth]&&![auth isEqualToString:EMPTY_STRING])
    {
        _auth = auth;
        _didLogin = YES;
        [self save];
    }
}


-(BOOL)save
{
    NSData *myEncodedObj = [NSKeyedArchiver archivedDataWithRootObject:self];
    [USERDEFAULT setObject:myEncodedObj forKey:USER_INFO_FLAG];
    BOOL sync = [USERDEFAULT synchronize];
    return sync;
}



#pragma mark ---Custom decode/encode
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        
        decodeObject(_auth);
        decodeObject(_uid);
        decodeObject(_passport);
        decodeObject(_name);
        decodeObject(_avatar);
        decodeObject(_tAvatar);
        decodeObject(_iphoneId);
        decodeObject(_register_time);
        decodeObject(_city);
        decodeObject(_status);

        decodeBool(_didLogin);
        decodeBool(_canPush);
        decodeBool(_canAlert);
        decodeBool(_canSound);
        decodeBool(_canVibrator);
        decodeFloat(_timeStamp);
        
        }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
    encodeObject(_auth);
    encodeObject(_uid);
    encodeObject(_passport);
    encodeObject(_name);
    encodeObject(_avatar);
    encodeObject(_tAvatar);
    encodeObject(_iphoneId);
    encodeObject(_register_time);
    encodeObject(_city);
    encodeObject(_status);
    encodeBool(_didLogin);
    encodeBool(_canPush);
    encodeBool(_canAlert);
    encodeBool(_canSound);
    encodeBool(_canVibrator);
    encodeFloat(_timeStamp);
}


@end
